package com.sihan.swvp.action.sjfx;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.service.sjfx.LeadDrivingService;

@Controller
@RequestMapping("/ldjs")
public class LdjsAction extends BaseAction{
	@Autowired
	private LeadDrivingService leadDrivingService;
	
	
	//今日任务总数（巡视任务、工作任务）
		@ResponseBody
		@RequestMapping("/taskSum_today") 
		public String taskSum_today(){
			List<Map<String, Object>> list = leadDrivingService.taskSum_today();
			StringBuffer sb = new StringBuffer();
			sb.append("<chart caption='' formatNumberScale='0' basefontsize='11.5' showValues='0'>");
			int sum = 0;
			for (int i = 0;i < list.size();i++) {
				Integer v = CommonUtil.o2int(list.get(i).get("counts"));
				if(v != null) sum += v;
			}
			
			for (int i = 0;i < list.size();i++) {
				Integer v = CommonUtil.o2int(list.get(i).get("counts"));
				String tip = list.get(i).get("name")+","+(sum > 0 ? CommonUtil.numberFormat(v*100/sum, 2) : 0)+"%";
				if(null != v && v > 0){
					sb.append("<set label='"+list.get(i).get("name")+","+v+"' value='"+v+"' toolText='"+tip+"'/>");
				}
				else{
					sb.append("<set label='"+list.get(i).get("name")+",0' value='0.001' toolText='"+tip+"'/>");
				}
				
			}
			sb.append("</chart>");
			
			return sb.toString();
		} 
		
		//今日视频总数［关联视频、同步视频]
		@ResponseBody
		@RequestMapping("/videoSum_today") 
		public String videoSum_today(){
			List<Map<String, Object>> list = leadDrivingService.videoSum_today();
			StringBuffer sb = new StringBuffer();
			sb.append("<chart basefontsize='11.5' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
			for (int i = 0;i < list.size();i++) {
				sb.append("<set label='"+list.get(i).get("name")+"' value='"+list.get(i).get("counts")+"' />");
			}
			sb.append("</chart>");
			
			return sb.toString();
		}
		 
		//今日任务完成率（巡视完成率、工作完成率）
		@ResponseBody
		@RequestMapping("/taskLv_today") 
		public String taskLv_today(){
			List<Map<String, Object>> list = leadDrivingService.taskLv_today();
			StringBuffer sb = new StringBuffer();
			sb.append("<chart basefontsize='11.5' yAxisName='' numberSuffix='%' yAxisMaxValue='100' caption='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
			for (int i = 0;i < list.size();i++) {
				Object lv = list.get(i).get("lv") == null?0:list.get(i).get("lv");
				sb.append("<set label='"+list.get(i).get("name")+"' value='"+CommonUtil.numberFormat(lv, 1) +"' />");
			}
			sb.append("</chart>");
			
			return sb.toString();
		}
		
		//<!-- 本周每日视频同步数量 -->
		@ResponseBody
		@RequestMapping("/videoSyn_week")
		public String videoSyn_week(){
			List<Map<String, Object>> list = leadDrivingService.videoSyn_week();
			StringBuffer sb = new StringBuffer();
			sb.append("<chart basefontsize='11.5' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
			
			for (int i = 0;i < list.size();i++) {
				String week = this.week(Integer.valueOf(list.get(i).get("wk").toString()));
				Object counts = list.get(i).get("counts") == null?0:list.get(i).get("counts");
				sb.append("<set label='"+week+"' value='"+counts+"' toolText = '数量,"+week+","+counts+"'/>");
			}
			sb.append("</chart>");
			
			return sb.toString();
		}
		
		//<!-- 本周每日视频违规数量及违规率 -->
		@ResponseBody
		@RequestMapping("/videoWd_week")
		public String videoWd_week(){
			List<Map<String, Object>> list = leadDrivingService.videoWd_week();
			StringBuffer sb = new StringBuffer();
			sb.append("<chart basefontsize='11.5' palette='4' pYAxisName='数量' sYAxisName='违规率（%）' syAxisMaxValue='100' showValues='0' rotateLabels='1' slantLabels='1' plotSpacePercent='40' sNumberSuffix='%' >");
			sb.append("<categories>");
			for (int i = 0; i < list.size(); i++) {
				sb.append(" <category label='"+this.week(Integer.valueOf(list.get(i).get("wk").toString()))+"' />");
			}
			sb.append("</categories>");
			sb.append("<dataset seriesName='数量'>");
			for (int i = 0; i < list.size(); i++) {
				Object counts = list.get(i).get("counts") == null?0:list.get(i).get("counts");
				sb.append("<set value='"+counts+"' />");;
			}
			sb.append("</dataset>");
			sb.append("<dataset seriesname='违规率' parentYAxis='S' color='50BD4A'>");
			for (int i = 0; i < list.size(); i++) {
				Object lv = list.get(i).get("lv") == null?0:list.get(i).get("lv");
				sb.append("<set value='"+new DecimalFormat("#.0").format(lv) +"' />");
			}
			sb.append("</dataset>");
			
			sb.append("</chart>");
			return sb.toString();
		}
		
		//<!-- 本周每日视频关联数量 -->
		@ResponseBody
		@RequestMapping("/videoManager_week")
		public String videoManager_week(){
			List<Map<String, Object>> list = leadDrivingService.videoManager_week();
			StringBuffer sb = new StringBuffer();
			sb.append("<chart basefontsize='11.5' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
			for (int i = 0;i < list.size();i++) {
				String week = this.week(Integer.valueOf(list.get(i).get("wk").toString()));
				Object counts = list.get(i).get("counts") == null?0:list.get(i).get("counts");
				sb.append("<set label='"+week+"' value='"+counts+"' toolText = '数量,"+week+","+counts+"'/>");
			}
			sb.append("</chart>");
			return sb.toString();
		}
		
		//<!-- 本月每日视频同步数量  -->
		@ResponseBody
		@RequestMapping("/videoSyn_month")
		public String videoSyn_month(){
			List<Map<String, Object>> list = leadDrivingService.videoSyn_month();
			
			StringBuffer sb = new StringBuffer();
			sb.append("<chart basefontsize='11.5' XAxisName='' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
			for (int i = 0;i < list.size();i++) {
				sb.append("<set label='"+list.get(i).get("days")+"' value='"+list.get(i).get("counts")+"' />");
			}
			sb.append("</chart>");
			return sb.toString();
		}
		
		//<!-- 近30日每日视频违规数量及违规率 -->
		@ResponseBody
		@RequestMapping("/videoWd_month")
		public String videoWd_month(){
			List<Map<String, Object>> list = leadDrivingService.videoWd_month();
			GregorianCalendar c = new GregorianCalendar();
		    c.add(Calendar.DAY_OF_MONTH, -30);
			List<String> times = CommonUtil.getDatesBetweenTwoDate(c.getTime(), new Date());
			
			StringBuffer sb = new StringBuffer();
			sb.append("<chart basefontsize='11.5' showToolTip='1' palette='4' pYAxisName='数量' sYAxisName='违规率（%）' showValues='0' syAxisMaxValue='100' rotateLabels='1' slantLabels='1' plotSpacePercent='40' sNumberSuffix='%' >");
			sb.append("<categories>");
			for (String s : times) {
				c.setTime(CommonUtil.o2date(s, "yyyy-MM-dd"));
				sb.append("<category label='"+(c.get(c.MONTH) + 1)+"月"+c.get(c.DAY_OF_MONTH)+"' />");
			}
			sb.append("</categories>");
			sb.append("<dataset seriesName='数量'>");
			for (String s : times) {
				for (int i = 0; i < list.size(); i++) {
					String starttime = list.get(i).get("starttime").toString();
					if(s.equals(starttime)){
						sb.append("<set value='"+list.get(i).get("counts")+"' toolText='违规数量："+list.get(i).get("counts")+"'/>");
						break;
					}
					if(list.size() == i + 1){
						sb.append("<set value='0' />");
					}
				}
			}
			sb.append("</dataset>");
			sb.append("<dataset seriesname='违规率' parentYAxis='S' color='50BD4A'>");
			for (String s : times) {
				for (int i = 0; i < list.size(); i++) {
					String starttime = list.get(i).get("starttime").toString();
					if(s.equals(starttime)){
						String lv = new DecimalFormat("#.0").format(list.get(i).get("lv"));
						sb.append("<set value='"+lv+"' toolText='违规率："+lv+"%'/>");
						break;
					}
					if(list.size() == i + 1){
						sb.append("<set value='0' />");
					}
				}
			}
			sb.append("</dataset>");
			
			sb.append("</chart>");
			return sb.toString();
		}
		
		//<!-- 本月每日视频关联数量 -->
		@Deprecated
		@ResponseBody
		@RequestMapping("/videoManager_month")
		public Object videoManager_month(){
			List<Map<String, Object>> list = leadDrivingService.videoManager_month();
			
			GregorianCalendar c = new GregorianCalendar();
		    c.add(Calendar.DAY_OF_MONTH, -30);
			List<String> times = CommonUtil.getDatesBetweenTwoDate(c.getTime(), new Date());
			
			StringBuffer sb = new StringBuffer();
			//sb.append("<chart basefontsize='11.5' XAxisName='' yAxisName='数量' caption='' numberPrefix='' showBorder='1' imageSave='1' imageSaveURL='http://www.fusioncharts.com/ExportHandlers/PHP/_FCExporter.php'>");
			
			for (String s : times) {
				c.setTime(CommonUtil.o2date(s, "yyyy-MM-dd"));
				for (int i = 0;i < list.size();i++) {
					String matchtime = list.get(i).get("match_time").toString();
					/*if(s.equals(matchtime)){
						sb.append("<set label='"+c.get(c.DAY_OF_MONTH)+"' value='"+list.get(i).get("counts")+"' />");
					}else{
						sb.append("<set label='"+c.get(c.DAY_OF_MONTH)+"' value='0' />");
					}*/
					if(s.equals(matchtime)){
						sb.append("<set value='"+list.get(i).get("counts")+"' />");
					}else{
						sb.append("<set value='0' />");
					}
					
				}
			}
			//sb.append("</chart>");
			return sb;
		}
		
		//<!-- 近30日视频同步量和视频关联量 -->
		@ResponseBody
		@RequestMapping("/thisMonthVM") 
		public Object thisMonthVideosynAndVideoManager(){
			StringBuffer sb = new StringBuffer();
			//视频同步量
			List<Map<String, Object>> sync = leadDrivingService.videoSyn_month();
			//视频关联量
			List<Map<String, Object>> manager = leadDrivingService.videoManager_month();
			GregorianCalendar c = new GregorianCalendar();
		    c.add(Calendar.DAY_OF_MONTH, -30);
			List<String> times = CommonUtil.getDatesBetweenTwoDate(c.getTime(), new Date());
			
			sb.append("<chart showValues = '1' showToolTip='1' basefontsize='11.5' palette='5' caption='' xAxisName='' yAxisName='数量' numberPrefix=''  rotateValues='1' placeValuesInside='' forceYAxisValueDecimals='1'  yAxisValueDecimals='0'>");
			sb.append("<categories>");
			for (String s : times) {
				c.setTime(CommonUtil.o2date(s, "yyyy-MM-dd"));
				sb.append("<category label='"+(c.get(c.MONTH) + 1)+"月"+c.get(c.DAY_OF_MONTH)+"' />");
			}
			sb.append("</categories>");
			sb.append("<dataset seriesname='视频关联' color=''  showValues='1'>");
			//视频关联量
			for (String s : times) {
				for (int i = 0;i < manager.size();i++) {
					String matchtime = manager.get(i).get("match_time").toString();
					if(s.equals(matchtime)){
						sb.append("<set value='"+manager.get(i).get("counts")+"' toolText = '视频关联："+manager.get(i).get("counts")+"' />");
						break;
					}
					if(manager.size() == i + 1){
						sb.append("<set value='0' />");
					}
					
				}
			}
			//sb.append(this.videoManager_month());
			sb.append("</dataset>");
			sb.append("<dataset seriesname='视频同步' color=''  showValues='1'>");
			for (String s : times) {
				for (int i = 0;i < sync.size();i++) {
					String syntime = sync.get(i).get("syntime").toString();
					if(s.equals(syntime)){
						sb.append("<set value='"+sync.get(i).get("counts")+"' toolText = '视频同步："+sync.get(i).get("counts")+"' />");
						break;
					}
					if(sync.size() == i + 1){
						sb.append("<set value='0' />");
					}
					
				}
			}
			sb.append("</dataset>");
			sb.append("</chart>");
			
			return sb.toString();
		}
		
		
		//<!-- 巡视任务chart -->
		@ResponseBody
		@RequestMapping("/getXschart") 
		public String getXschart(){
			List<Map<String, Object>> list = leadDrivingService.getXschart();
			
			StringBuffer sb = new StringBuffer();
			sb.append("<chart basefontsize='11.5' palette='4' XAxisName=''  pYAxisName='同步数量' sYAxisName='完成率（%）' syAxisMaxValue='100' showValues='0' rotateLabels='1' slantLabels='1' plotSpacePercent='40' sNumberSuffix='%' >");
			sb.append("<categories>");
			for (int i = 0; i < list.size(); i++) {
				sb.append(" <category label='"+list.get(i).get("month")+"月' />");
			}
			sb.append("</categories>");
			sb.append("<dataset seriesName='数量' color='CC3300'>");
			for (int i = 0; i < list.size(); i++) {
				Object counts = list.get(i).get("counts") == null?0:list.get(i).get("counts");
				sb.append("<set value='"+counts+"' />");;
			}
			sb.append("</dataset>");
			sb.append("<dataset seriesname='完成率' parentYAxis='S' color='CC3300'>");
			for (int i = 0; i < list.size(); i++) {
				Object flv = list.get(i).get("flv") == null?0:list.get(i).get("flv");
				sb.append("<set value='"+CommonUtil.numberFormat(flv, 1)+"' />");
			}
			sb.append("</dataset>");
			sb.append("</chart>");
			
			return sb.toString();
		}

		//<!-- 工作任务任务chart -->
		@ResponseBody
		@RequestMapping("/getWorkchart") 
		public String getWorkchart(){
			List<Map<String, Object>> list = leadDrivingService.getWorkchart();
			StringBuffer sb = new StringBuffer();
			sb.append("<chart basefontsize='11.5' palette='4' XAxisName=''  pYAxisName='同步数量' sYAxisName='完成率（%）' syAxisMaxValue='100' showValues='0' rotateLabels='1' slantLabels='1' plotSpacePercent='40' sNumberSuffix='%' >");
			sb.append("<categories>");
			for (int i = 0; i < list.size(); i++) {
				sb.append(" <category label='"+list.get(i).get("month")+"月' />");
			}
			sb.append("</categories>");
			sb.append("<dataset seriesName='数量' color='CC3300'>");
			for (int i = 0; i < list.size(); i++) {
				Object counts = list.get(i).get("counts") == null?0:list.get(i).get("counts");
				sb.append("<set value='"+counts+"' />");;
			}
			sb.append("</dataset>");
			sb.append("<dataset seriesname='完成率' parentYAxis='S' color='CC3300'>");
			for (int i = 0; i < list.size(); i++) {
				Object flv = list.get(i).get("flv") == null?0:list.get(i).get("flv");
				sb.append("<set value='"+CommonUtil.numberFormat(flv, 1)+"' />");
			}
			sb.append("</dataset>");
			
			sb.append("</chart>");
			return sb.toString();
		}
		
		public String week(int v){
			String valeu = "";
			switch (v) {
			case 0:
				valeu = "星期一";
				break;
			case 1:
				valeu = "星期二";
				break;
			case 2:
				valeu = "星期三";
				break;
			case 3:
				valeu = "星期四";
				break;
			case 4:
				valeu = "星期五";
				break;
			case 5:
				valeu = "星期六";
				break;
			case 6:
				valeu = "星期日";
				break;
			}
			return valeu;
		}
		

}
